package com.ly.order.controller;

import com.common.core.domain.AjaxResult;
import com.common.core.text.UUID;
import com.common.core.web.controller.BaseController;
import com.ly.order.producer.OrderProducer;
import com.ly.order.producer.OrderTransactionProducer;
import com.ly.order.service.OrderService;
import com.domain.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/orderCtrl")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;


    @Autowired
    private OrderProducer orderProducer;

    @Autowired
    private OrderTransactionProducer orderTransactionProducer;



    @GetMapping("/queryLocalhost")
    public AjaxResult queryLocalhost(Order order){
        return AjaxResult.success(orderService.queryLocalhost(order));
    }

    //测试普通生产者
    @PostMapping("/createOrder")
    public AjaxResult createOrder(@RequestBody Order order){
        try {
            orderProducer.sendMsg();
        }catch (Exception e){
            e.printStackTrace();
        }
        return toAjax(1);
    }


    //测试普通批量生产者
    @PostMapping("/createBatchOrder")
    public AjaxResult createBatchOrder(@RequestBody Order order){
        try {
            orderProducer.batchSendMsg();
        }catch (Exception e){
            e.printStackTrace();
        }
        return toAjax(1);
    }


    //测试顺序消费者
    @PostMapping("createIndexOrder")
    public AjaxResult createIndexOrder(@RequestBody Order order){
        try {
            orderProducer.sendIndexMsg();
        }catch (Exception e){
            e.printStackTrace();
        }
        return toAjax(1);
    }




    //测试事务类型的生产者
    @PostMapping("/createTransactionOrder")
    public AjaxResult createTransactionOrder1(@RequestBody Order order){
        String id = UUID.randomUUID().toString();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        new Order().setId(id).setDate(sdf.format(new Date())).setNum("1");
        orderTransactionProducer.send(id,new Order());
        return toAjax(1);
    }





    //测试
    @GetMapping("/sendMqTest")
    public AjaxResult sendMqTest(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        defaultProducer.send(MQTopic.MESSAGE_TRACING_ONE,UUID.randomUUID().toString(),
//                JSON.toJSONString(new Order().setDate(sdf.format(new Date()))));
//        defaultProducer.send(MQTopic.MESSAGE_TRACING_TWO,UUID.randomUUID().toString(),
//                JSON.toJSONString(new Order().setDate(sdf.format(new Date()))));
        return AjaxResult.success();
    }



}
